home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / TIME_ZON / CLISTBOX.C next >
Text File  |  1991-11-08  |  4KB  |  262 lines

  1. /* CListBox.c */
  2.  
  3. #include "CListBox.h"
  4. #include <CList.h>
  5. #include <GenRoutines.h>
  6. #include <Packages.h>
  7. #include <math.h>
  8.  
  9. void
  10. CListBox::IListBox(owner,view,itemCount, dItemNo)
  11. WindowPtr owner;
  12. Rect *view;
  13. int itemCount;
  14. int    dItemNo;
  15. {
  16.     Rect db;
  17.     Point cellsize;
  18.     
  19.         /*
  20.          * Make up cells of an appropriate size for a list:
  21.          */
  22.     
  23.     copy_rectangle(view,&dRect);
  24.     SetRect(&db,0,0,1,itemCount);
  25.     cellsize.h = dRect.right - dRect.left;
  26.     cellsize.v = 16;
  27.     itemNo = dItemNo;
  28.     
  29.         /*
  30.          * Make a new list, for this window/dialog:
  31.          */
  32.     
  33.     theWind = owner;
  34.     theList = LNew(&dRect,&db,cellsize,0,theWind,TRUE,FALSE,
  35.                         FALSE,TRUE);
  36. }
  37.  
  38.  
  39. void
  40. CListBox::Dispose()
  41. {
  42.         /*
  43.          * Get rid of the list, which exists in its own right:
  44.          */
  45.  
  46.     LDispose(theList);
  47.     
  48.     inherited::Dispose();
  49. }
  50.  
  51.  
  52. void
  53. CListBox::SetCell(position,string)
  54. int position;
  55. char *string;
  56. {
  57.     Cell which;
  58.     
  59.         /*
  60.          * There's only one dimension, y, so set 0,pos-1 to this string:
  61.          */
  62.     
  63.     which.h = 0;
  64.     which.v = position - 1;
  65.     LSetCell(string,strlen(string),which,theList);
  66. }
  67.  
  68.  
  69. void
  70. CListBox::InsertCell(position,string)
  71. int position;
  72. char *string;
  73. {
  74.     Cell which;
  75.     
  76.         /*
  77.          * Put the new cell in the one-dimension array at pos-1:
  78.          */
  79.     
  80.     which.h = 0;
  81.     which.v = position - 1;
  82.     LAddRow(1,position - 1,theList);
  83.     LSetCell(string,strlen(string),which,theList);
  84. }
  85.  
  86.  
  87. void
  88. CListBox::RemoveCell(position)
  89. int position;
  90. {
  91.     LDelRow(1,position-1,theList);
  92. }
  93.  
  94.  
  95. void
  96. CListBox::GetCell(string)
  97. char *string;
  98. {
  99.     Cell which;
  100.     int length;
  101.     
  102.         /*
  103.          * If no cell is currently selected, return a null string,
  104.          * else return that cell's string. Note that the string is
  105.          * owned by the caller, and is expected to be char[256].
  106.          */
  107.     
  108.     which.h = which.v = 0;
  109.     if (!LGetSelect((Boolean) TRUE,&which,theList))
  110.         string[0] = '\0';
  111.     else {
  112.         length = 255;
  113.         LGetCell(string,&length,which,theList);
  114.         string[length] = '\0';
  115.     }
  116. }
  117.  
  118. void
  119. CListBox::ClearSelect()
  120. {
  121.     Cell which;
  122.     
  123.         /*
  124.          * If any cell is selected, make it unselected:
  125.          */
  126.     
  127.     which.h = which.v = 0;
  128.     if (LGetSelect((Boolean) TRUE,&which,theList))
  129.         LSetSelect((Boolean) FALSE, which, theList);
  130.         
  131. }
  132.  
  133.  
  134. void
  135. CListBox::Draw()
  136. {
  137.         /*
  138.          * This only needs to put a frame around it.
  139.          */
  140.     
  141.     PenSize(1,1);
  142.     InsetRect(&dRect,-1,-1);
  143.     FrameRect(&dRect);
  144.     InsetRect(&dRect,1,1);
  145. }
  146.  
  147.  
  148. void
  149. CListBox::Update()
  150. {
  151.         /*
  152.          * The List Manager will take care of the main display of the
  153.          * list.
  154.          */
  155.  
  156.     LUpdate(theWind->visRgn,theList);
  157. }
  158.  
  159.  
  160. Boolean
  161. CListBox::IsIn(where)
  162. Point where;
  163. {
  164.     Rect temp;
  165.     
  166.         /*
  167.          * See if the point is within the list box, including the
  168.          * scroll bar:
  169.          */
  170.     
  171.     copy_rectangle(&dRect,&temp);
  172.     temp.right += 16;
  173.     return(PtInRect(where,&temp));
  174. }
  175.  
  176.  
  177. void
  178. CListBox::DoClick(where,what)
  179. Point where;
  180. EventRecord *what;
  181. {
  182.     Boolean inwhat;
  183.     
  184.         /*
  185.          * The List Manager knows how to do this.
  186.          */
  187.     
  188.     inwhat = LClick(where,what->modifiers,theList);
  189. }
  190.  
  191.  
  192. /********************************************************************/
  193.  
  194. pascal void
  195. draw_list(thedlg,which)
  196. WindowPtr thedlg;
  197. int which;
  198. {
  199.     CList    *theLBList;
  200.     CListBox *theLBox;
  201.     Rect temp;
  202.     int        count, index;
  203.     
  204.     theLBList = (CList *) GetWRefCon(thedlg);
  205.     count = theLBList->GetNumItems();
  206.     
  207.     for (index = 1; index <= count; index++) {
  208.         theLBox = (CListBox *)theLBList->NthItem(index);
  209.         theLBox->Update();
  210.         theLBox->Draw();
  211.     }
  212. }
  213.  
  214.  
  215. pascal Boolean
  216. check_list(thedlg,evtptr,hitptr)
  217. DialogPtr thedlg;
  218. EventRecord *evtptr;
  219. int *hitptr;
  220. {
  221.     Boolean myres, inwhat;
  222.     long keyval;
  223.     Point mouseloc;
  224.     CListBox *theLBox;
  225.     Rect temp;
  226.     void copy_rectangle();
  227.     CList    *theLBList;
  228.     int        count, index;
  229.     
  230.     myres = FALSE;
  231.     switch (evtptr->what) {
  232.         case keyDown:
  233.         case autoKey:
  234.             keyval = BitAnd(evtptr->message,charCodeMask);
  235.             if ((keyval == 3) || (keyval == 13)) {
  236.                 myres = TRUE;
  237.                 *hitptr = 1;
  238.             }
  239.             break;
  240.         case mouseDown:
  241.             mouseloc = evtptr->where;
  242.             GlobalToLocal(&mouseloc);
  243.             theLBList = (CList *) GetWRefCon(thedlg);
  244.             count = theLBList->GetNumItems();
  245.             for (index = 1; index <= count; index++) {
  246.                 theLBox = (CListBox *)theLBList->NthItem(index);
  247.     
  248.                 copy_rectangle(&(theLBox->dRect),&temp);
  249.                 temp.right += 16;
  250.                 if (theLBox->IsIn(mouseloc)) {
  251.                     theLBox->DoClick(mouseloc,evtptr);
  252.                     *hitptr = theLBox->itemNo;
  253.                     myres = TRUE;
  254.                 }
  255.             }
  256.             break;
  257.     }
  258.     return(myres);
  259. }
  260.  
  261.  
  262.